package com.yuyou.fn.timer.task;

import com.github.wens.mybatisplus.examples.Example;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.yuyou.fn.settlement.constant.Subjects;
import com.yuyou.fn.settlement.entity.MainAccount;
import com.yuyou.fn.settlement.service.IMainAccountService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 结算实时余额"
 * Created by wens on 2017/4/19.
 */
@JobHander(value="CalRealtimeBalanceTask")
@Component
public class CalRealtimeBalanceTask extends IJobHandler {


    @Resource
    private IMainAccountService mainAccountService ;


    @Override
    public ReturnT<String> execute(String... params) throws Exception {



        Example<MainAccount> example = MainAccount.createExample();
        example.createCriteria().andIn("tag", Arrays.asList("areaTeam" ,"busTeam" ,"teacher"));
        List<MainAccount> mainAccountList = mainAccountService.findListByExample(example);

        Date firsDayOfYear = null ;
        Date lastDayOfYear = null ;
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        firsDayOfYear = calendar.getTime() ;
        calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        lastDayOfYear = calendar.getTime();

        for(MainAccount mainAccount : mainAccountList ){

            BigDecimal income = mainAccountService.sumNotSettleAmount(mainAccount.getMainAccountId(),Subjects.REAL_INCOME ,firsDayOfYear,lastDayOfYear);
            BigDecimal cost = mainAccountService.sumNotSettleAmount(mainAccount.getMainAccountId(),Subjects.COST ,firsDayOfYear,lastDayOfYear);

            if(income == null ){
                income = BigDecimal.ZERO ;
            }
            if(cost == null ){
                cost = BigDecimal.ZERO ;
            }


            updateMainAccountRealBalance(mainAccount, income, cost);

        }
        return ReturnT.SUCCESS ;
    }

    private void updateMainAccountRealBalance(MainAccount mainAccount, BigDecimal income, BigDecimal cost) {
        MainAccount update = new MainAccount() ;
        update.setMainAccountId(mainAccount.getMainAccountId());
        BigDecimal balance = mainAccountService.calBalance(mainAccount , income.subtract(cost));
        //粗略计算
        update.setRealtimeBalanceAmount(mainAccount.getBalanceAmount().add(balance));
        mainAccountService.updateSelectiveById(update);
    }

}
